package ksj.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.Schedules;
import ksj.common.utils.net.Status;
import ksj.common.web.BaseWebService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Controller
@EntityScan(basePackages = { "bean" })
public class SchedulerController extends BaseWebService {
	private final Logger log = LoggerFactory.getLogger(this.getClass());
	// ResponseBody是使用刚才添加的gson依赖让这个函数返回的内容格式为json
	@Autowired
	private SchedulesRepository schedulerDAO;

	@GetMapping(path = "/getSchedules")
	public @ResponseBody Response getSchedules() {
		log.info("got request /getSchedules");
		System.out.println("got request /getSchedules");
		try {
			List<Object[]> schedules = schedulerDAO.getAllSchedules();
			List<Schedules> list = new ArrayList<>();
			for (Object[] objects : schedules) {
				Schedules schedule = new Schedules();
				if (objects[0] != null) {
					schedule.setTask_name(objects[0].toString());
				}
				if (objects[1] != null) {
					schedule.setTask_type(objects[1].toString());
				}
				if (objects[2] != null) {
					schedule.setConfig_json(objects[2].toString());
				}
				if (objects[3] != null) {
					schedule.setVariables(objects[3].toString());
				}
				if (objects[4] != null) {
					schedule.setMessage(objects[4].toString());
				}
				if (objects[5] != null) {
					schedule.setSchedule_type(objects[5].toString());
				}
				if (objects[6] != null) {
					schedule.setWeek_day(objects[6].toString());
				}
				if (objects[7] != null) {
					schedule.setMonth_day(objects[7].toString());
				}
				if (objects[8] != null) {
					// DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
					// java.sql.Time timeValue = null;
					// try {
					// timeValue = new
					// java.sql.Time(formatter.parse(objects[8].toString()).getTime());
					// } catch (ParseException e) {
					// e.printStackTrace();
					// }
					schedule.setStart_time(objects[8].toString());
				}
				if (objects[9] != null) {
					schedule.setInterval(Integer.parseInt(objects[9].toString()));
				}
				if (objects[10] != null) {
					schedule.setIs_enabled(Boolean.valueOf(objects[10].toString()));
				}
				if (objects[11] != null) {
					schedule.setIs_deleted(Boolean.valueOf(objects[11].toString()));
				}
				if (objects[12] != null) {
					// DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
					// java.sql.Time timeValue = null;
					// try {
					// timeValue = new
					// java.sql.Time(formatter.parse(objects[12].toString()).getTime());
					// } catch (ParseException e) {
					// e.printStackTrace();
					// }
					schedule.setEnd_time(objects[12].toString());
				}
				if (objects[13] != null) {
					schedule.setId(Long.parseLong(objects[13].toString()));
				}
				list.add(schedule);

			}

			return createResponse(Status.RESPONSE_INFO_SUCCESS, Status.RESPONSE_MESSAGE_INFO_SUCCESS, list,
					schedules.size());

			// return new ResponseEntity<List<Schedules>>(list,
			// org.springframework.http.HttpStatus.OK);
		} catch (Exception e) {
			e.printStackTrace();
			return createResponse(Status.RESPONSE_INFO_FAILURE, Status.RESPONSE_MESSAGE_INFO_FAILURE, null);
		}
	}

}
